﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GoogleLatitude2SharePoint.LatitudeWebPart
{
    class LatitudeWebPartToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
    {
        TextBox tbBingMapsKey;
        TextBox tbLatitudeUserID;
        TextBox tbPropertyName;
        TextBox tbManualDisplayName;
        TextBox tbMapWidth;
        TextBox tbMapHeight;
        DropDownList ddlModes;
        DropDownList ddlDataSources;

        DropDownList ddlSites;
        DropDownList ddlLists;
        DropDownList ddlGroups;
        DropDownList ddlMapTypes;
        DropDownList ddlFields;
        DropDownList ddlNameFields;

        DropDownList ddlUser;
        DropDownList ddlUsers;

        TableRow rowLatitudeUserID;
        TableRow rowList;
        TableRow rowSite;
        TableRow rowField;
        TableRow rowPropertyName;
        TableRow rowGroups;
        TableRow rowManualDisplayName;
        TableRow rowNameFields;

        TableRow rowUser;
        TableRow rowUsers;

        protected override void CreateChildControls()
        {

            LatitudeWebPart webPart = (LatitudeWebPart)this.ParentToolPane.SelectedWebPart;

            this.Title = "Latitude 2 SharePoint";

            Table table = new Table();


            TableRow rowMapOptions = new TableRow();
            table.Controls.Add(rowMapOptions);
            TableCell cellMapOptions = new TableCell();
            rowMapOptions.Controls.Add(cellMapOptions);
            Label lblMapOptions = new Label();
            lblMapOptions.Text = "Map";
            lblMapOptions.Font.Bold = true;
            cellMapOptions.Controls.Add(lblMapOptions);
            
            
            
            TableRow rowBingMapsKey = new TableRow();
            table.Controls.Add(rowBingMapsKey);

            TableCell cellLblBingMapsKey = new TableCell();
            rowBingMapsKey.Controls.Add(cellLblBingMapsKey);
            Label lblBingMapsKey = new Label();
            lblBingMapsKey.Text = "Bing Maps Key";
            cellLblBingMapsKey.Controls.Add(lblBingMapsKey);

            TableCell cellTbBingMapsKey = new TableCell();
            rowBingMapsKey.Controls.Add(cellTbBingMapsKey);
            tbBingMapsKey = new TextBox();
            tbBingMapsKey.Text = webPart.BingMapsKey;
            tbBingMapsKey.Width = Unit.Percentage(100);
            cellTbBingMapsKey.Controls.Add(tbBingMapsKey);



            TableRow rowMapTypes = new TableRow();
            table.Controls.Add(rowMapTypes);

            TableCell cellLblMapTypes = new TableCell();
            rowMapTypes.Controls.Add(cellLblMapTypes);
            Label lblMapTypes = new Label();
            lblMapTypes.Text = "Type";
            cellLblMapTypes.Controls.Add(lblMapTypes);

            TableCell cellDdlMapTypes = new TableCell();
            rowMapTypes.Controls.Add(cellDdlMapTypes);
            ddlMapTypes = new DropDownList();
            ddlMapTypes.DataSource = webPart.MapOptions;
            ddlMapTypes.Width = Unit.Percentage(100);
            ddlMapTypes.SelectedValue = webPart.CurrentMapType.ToString();
            ddlMapTypes.DataTextField = "Type";
            ddlMapTypes.DataValueField = "Type";

            ddlMapTypes.DataBind();

            cellDdlMapTypes.Controls.Add(ddlMapTypes);






            TableRow rowMapWidth = new TableRow();
            table.Controls.Add(rowMapWidth);

            TableCell cellLblMapWidth = new TableCell();
            rowMapWidth.Controls.Add(cellLblMapWidth);
            Label lblMapWidth = new Label();
            lblMapWidth.Text = "Width";
            cellLblMapWidth.Controls.Add(lblMapWidth);

            TableCell cellTbMapWidth = new TableCell();
            rowMapWidth.Controls.Add(cellTbMapWidth);
            tbMapWidth = new TextBox();
            tbMapWidth.Text = webPart.MapWidth.ToString();
            tbMapWidth.Width = Unit.Percentage(100);
            cellTbMapWidth.Controls.Add(tbMapWidth);




            TableRow rowMapHeight = new TableRow();
            table.Controls.Add(rowMapHeight);

            TableCell cellLblMapHeight = new TableCell();
            rowMapHeight.Controls.Add(cellLblMapHeight);
            Label lblMapHeight = new Label();
            lblMapHeight.Text = "Height";
            cellLblMapHeight.Controls.Add(lblMapHeight);

            TableCell cellTbMapHeight = new TableCell();
            rowMapHeight.Controls.Add(cellTbMapHeight);
            tbMapHeight = new TextBox();
            tbMapHeight.Text = webPart.MapHeight.ToString();
            tbMapHeight.Height = Unit.Percentage(100);
            cellTbMapHeight.Controls.Add(tbMapHeight);




            TableRow rowDataSourceOptions = new TableRow();
            table.Controls.Add(rowDataSourceOptions);
            TableCell cellDataSourceOptions = new TableCell();
            rowDataSourceOptions.Controls.Add(cellDataSourceOptions);
            Label lblDataSourceOptions = new Label();
            lblDataSourceOptions.Text = "Data Source";
            lblDataSourceOptions.Font.Bold = true;
            cellDataSourceOptions.Controls.Add(lblDataSourceOptions);




            TableRow rowMode = new TableRow();
            table.Controls.Add(rowMode);

            TableCell cellLblMode = new TableCell();
            rowMode.Controls.Add(cellLblMode);
            Label lblMode = new Label();
            lblMode.Text = "Mode";
            cellLblMode.Controls.Add(lblMode);

            TableCell cellDdlModes = new TableCell();
            rowMode.Controls.Add(cellDdlModes);
            ddlModes = new DropDownList();
            ddlModes.DataSource = webPart.Modes;
            ddlModes.Width = Unit.Percentage(100);
            ddlModes.SelectedValue = webPart.CurrentMode.ToString();

            ddlModes.AutoPostBack = true;
            ddlModes.SelectedIndexChanged += new EventHandler(ddlModes_SelectedIndexChanged);
            ddlModes.DataTextField = "Name";
            ddlModes.DataValueField = "Type";

            ddlModes.DataBind();

            cellDdlModes.Controls.Add(ddlModes);







            TableRow rowDataSource = new TableRow();
            table.Controls.Add(rowDataSource);

            TableCell cellLblDataSource = new TableCell();
            rowDataSource.Controls.Add(cellLblDataSource);
            Label lblDataSource = new Label();
            lblDataSource.Text = "Data Source";
            cellLblDataSource.Controls.Add(lblDataSource);

            TableCell cellDdlDataSource = new TableCell();
            rowDataSource.Controls.Add(cellDdlDataSource);
            ddlDataSources = new DropDownList();
            ddlDataSources.Width = Unit.Percentage(100);

            ddlDataSources.DataSource = webPart.DataSources((LatitudeWebPart.ModeTypes)Enum.Parse(typeof(LatitudeWebPart.ModeTypes), ddlModes.SelectedValue));
            ddlDataSources.DataTextField = "Name";
            ddlDataSources.DataValueField = "Type";
            ddlDataSources.AutoPostBack = true;
            ddlDataSources.SelectedIndexChanged += new EventHandler(ddlDataSources_SelectedIndexChanged);
            ddlDataSources.SelectedValue = webPart.CurrentDataSource.ToString();


            ddlDataSources.DataBind();




            cellDdlDataSource.Controls.Add(ddlDataSources);



            rowUser = new TableRow();
            table.Controls.Add(rowUser);

            TableCell cellLblUser = new TableCell();
            rowUser.Controls.Add(cellLblUser);
            Label lblUser = new Label();
            lblUser.Text = "User";
            cellLblUser.Controls.Add(lblUser);

            TableCell cellDdlUser = new TableCell();
            rowUser.Controls.Add(cellDdlUser);
            ddlUser = new DropDownList();
            ddlUser.Width = Unit.Percentage(100);

            ddlUser.DataSource = webPart.UserOptions;
            ddlUser.DataTextField = "Name";
            ddlUser.DataValueField = "ID";

            if (webPart.CurrentUser != LatitudeWebPart.UserTypes.Manual)
                ddlUser.SelectedValue = webPart.CurrentUser.ToString();
            else
                ddlUser.SelectedValue = webPart.CurrentUserLoginName;


            ddlUser.DataBind();




            cellDdlUser.Controls.Add(ddlUser);


            rowUsers = new TableRow();
            table.Controls.Add(rowUsers);

            TableCell cellLblUsers = new TableCell();
            rowUsers.Controls.Add(cellLblUsers);
            Label lblUsers = new Label();
            lblUsers.Text = "Users";
            cellLblUsers.Controls.Add(lblUsers);

            TableCell cellDdlUsers = new TableCell();
            rowUsers.Controls.Add(cellDdlUsers);
            ddlUsers = new DropDownList();
            ddlUsers.Width = Unit.Percentage(100);

            ddlUsers.DataSource = webPart.UsersOptions;
            ddlUsers.DataTextField = "Name";
            ddlUsers.DataValueField = "Type";
            ddlUsers.AutoPostBack = true;
            ddlUsers.SelectedIndexChanged += new EventHandler(ddlUsers_SelectedIndexChanged);
            ddlUsers.SelectedValue = webPart.CurrentUsers.ToString();


            ddlUsers.DataBind();




            cellDdlUsers.Controls.Add(ddlUsers);







            rowSite = new TableRow();
            table.Controls.Add(rowSite);

            TableCell cellLblSite = new TableCell();
            rowSite.Controls.Add(cellLblSite);
            Label lblSite = new Label();
            lblSite.Text = "Site";
            cellLblSite.Controls.Add(lblSite);

            TableCell cellDdlSite = new TableCell();
            rowSite.Controls.Add(cellDdlSite);
            ddlSites = new DropDownList();
            ddlSites.Width = Unit.Percentage(100);
            ddlSites.DataSource = webPart.Sites;
            ddlSites.DataTextField = "Title";
            ddlSites.DataValueField = "ID";
            ddlSites.AutoPostBack = true;
            ddlSites.SelectedIndexChanged += new EventHandler(ddlSites_SelectedIndexChanged);

            if (!string.IsNullOrEmpty(webPart.SourceSite))
                ddlSites.SelectedValue = webPart.SourceSite;



            ddlSites.DataBind();




            cellDdlSite.Controls.Add(ddlSites);

          //  rowSite.Visible = (currentDataSource == LatitudeWebPart.DataSourceTypes.List);



            rowGroups = new TableRow();
            table.Controls.Add(rowGroups);

            TableCell cellLblGroup = new TableCell();
            rowGroups.Controls.Add(cellLblGroup);
            Label lblGroup = new Label();
            lblGroup.Text = "Group";
            cellLblGroup.Controls.Add(lblGroup);

            TableCell cellDdlGroup = new TableCell();
            rowGroups.Controls.Add(cellDdlGroup);
            ddlGroups = new DropDownList();
            ddlGroups.Width = Unit.Percentage(100);
            ddlGroups.DataSource = webPart.Groups(new Guid(ddlSites.SelectedValue));
            ddlGroups.DataTextField = "Name";
            ddlGroups.DataValueField = "ID";

            if (!string.IsNullOrEmpty(webPart.SourceGroup))
                ddlGroups.SelectedValue = webPart.SourceGroup;



            ddlGroups.DataBind();




            cellDdlGroup.Controls.Add(ddlGroups);












            
            rowList = new TableRow();
            table.Controls.Add(rowList);

            TableCell cellLblList = new TableCell();
            rowList.Controls.Add(cellLblList);
            Label lblList = new Label();
            lblList.Text = "List";
            cellLblList.Controls.Add(lblList);

            TableCell cellDdlList = new TableCell();
            rowList.Controls.Add(cellDdlList);
            ddlLists = new DropDownList();
            ddlLists.Width = Unit.Percentage(100);
            ddlLists.DataSource = webPart.Lists(new Guid(ddlSites.SelectedValue));
            ddlLists.DataTextField = "Title";
            ddlLists.DataValueField = "ID";
            ddlLists.AutoPostBack = true;
            ddlLists.SelectedIndexChanged += new EventHandler(ddlLists_SelectedIndexChanged);

            if (!string.IsNullOrEmpty(webPart.SourceList))
                ddlLists.SelectedValue = webPart.SourceList;


            ddlLists.DataBind();


            cellDdlList.Controls.Add(ddlLists);




            rowField = new TableRow();
            table.Controls.Add(rowField);

            TableCell cellLblField = new TableCell();
            rowField.Controls.Add(cellLblField);
            Label lblField = new Label();
            lblField.Text = "User ID Field";
            cellLblField.Controls.Add(lblField);

            TableCell cellDdlField = new TableCell();
            rowField.Controls.Add(cellDdlField);
            ddlFields = new DropDownList();
            ddlFields.Width = Unit.Percentage(100);
            ddlFields.DataSource = webPart.Fields(new Guid(ddlSites.SelectedValue), new Guid(ddlLists.SelectedValue));
            ddlFields.DataTextField = "Title";
            ddlFields.DataValueField = "ID";

            if (!string.IsNullOrEmpty(webPart.SourceField))
                ddlFields.SelectedValue = webPart.SourceField;

            ddlFields.DataBind();


            cellDdlField.Controls.Add(ddlFields);



            
            rowNameFields = new TableRow();
            table.Controls.Add(rowNameFields);

            TableCell cellLblNameField = new TableCell();
            rowNameFields.Controls.Add(cellLblNameField);
            Label lblNameField = new Label();
            lblNameField.Text = "Name Field";
            cellLblNameField.Controls.Add(lblNameField);

            TableCell cellDdlNameField = new TableCell();
            rowNameFields.Controls.Add(cellDdlNameField);
            ddlNameFields = new DropDownList();
            ddlNameFields.Width = Unit.Percentage(100);
            ddlNameFields.DataSource = webPart.Fields(new Guid(ddlSites.SelectedValue), new Guid(ddlLists.SelectedValue));
            ddlNameFields.DataTextField = "Title";
            ddlNameFields.DataValueField = "ID";

            if (!string.IsNullOrEmpty(webPart.SourceNameField))
                ddlNameFields.SelectedValue = webPart.SourceNameField;

            ddlNameFields.DataBind();


            cellDdlNameField.Controls.Add(ddlNameFields);







            rowPropertyName = new TableRow();
            table.Controls.Add(rowPropertyName);

            TableCell cellLblPropertyName = new TableCell();
            rowPropertyName.Controls.Add(cellLblPropertyName);
            Label lblPropertyName = new Label();
            lblPropertyName.Text = "User ID Property Name";
            cellLblPropertyName.Controls.Add(lblPropertyName);

            TableCell cellTbPropertyName = new TableCell();
            rowPropertyName.Controls.Add(cellTbPropertyName);
            tbPropertyName = new TextBox();
            tbPropertyName.Width = Unit.Percentage(100);
            tbPropertyName.Text = webPart.PropertyName;

            cellTbPropertyName.Controls.Add(tbPropertyName);






           


            rowLatitudeUserID = new TableRow();
            table.Controls.Add(rowLatitudeUserID);

            TableCell cellLblLatitudeUserID = new TableCell();
            rowLatitudeUserID.Controls.Add(cellLblLatitudeUserID);
            Label lblLatitudeUserID = new Label();
            lblLatitudeUserID.Text = "User ID";
            cellLblLatitudeUserID.Controls.Add(lblLatitudeUserID);

            TableCell cellTbLatitudeUserID = new TableCell();
            rowLatitudeUserID.Controls.Add(cellTbLatitudeUserID);
            tbLatitudeUserID = new TextBox();
            tbLatitudeUserID.Width = Unit.Percentage(100);
            tbLatitudeUserID.Text = webPart.LatitudeUserID;

            cellTbLatitudeUserID.Controls.Add(tbLatitudeUserID);




            rowManualDisplayName = new TableRow();
            table.Controls.Add(rowManualDisplayName);

            TableCell cellLblManualDisplayName = new TableCell();
            rowManualDisplayName.Controls.Add(cellLblManualDisplayName);
            Label lblManualDisplayName = new Label();
            lblManualDisplayName.Text = "Name";
            cellLblManualDisplayName.Controls.Add(lblManualDisplayName);

            TableCell cellTbManualDisplayName = new TableCell();
            rowManualDisplayName.Controls.Add(cellTbManualDisplayName);
            tbManualDisplayName = new TextBox();
            tbManualDisplayName.Width = Unit.Percentage(100);
            tbManualDisplayName.Text = webPart.ManualDisplayName;

            cellTbManualDisplayName.Controls.Add(tbManualDisplayName);





            SetVisibilityRows();


            this.Controls.Add(table);

            base.CreateChildControls();
        }

        void ddlUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetVisibilityRows();
        }

        void ddlLists_SelectedIndexChanged(object sender, EventArgs e)
        {
            LatitudeWebPart webPart = (LatitudeWebPart)this.ParentToolPane.SelectedWebPart;

            ddlFields.DataSource = webPart.Fields(new Guid(ddlSites.SelectedValue), new Guid(ddlLists.SelectedValue));
            ddlFields.DataTextField = "Title";
            ddlFields.DataValueField = "ID";

            ddlFields.DataBind();


        }

        LatitudeWebPart.DataSourceTypes currentDataSource
        {
            get
            {
                return ((LatitudeWebPart.DataSourceTypes)Enum.Parse(typeof(LatitudeWebPart.DataSourceTypes), ddlDataSources.SelectedValue));
            }
        }

        LatitudeWebPart.UsersTypes currentUsersType
        {
            get
            {
                return ((LatitudeWebPart.UsersTypes)Enum.Parse(typeof(LatitudeWebPart.UsersTypes), ddlUsers.SelectedValue));
            }
        }

        LatitudeWebPart.UserTypes currentUserType
        {
            get
            {
                try
                {
                    return ((LatitudeWebPart.UserTypes)Enum.Parse(typeof(LatitudeWebPart.UserTypes), ddlUser.SelectedValue));
                }
                catch (ArgumentException)
                {
                    return LatitudeWebPart.UserTypes.Manual;
                }
            }
        }

        LatitudeWebPart.ModeTypes currentMode
        {
            get
            {
                return ((LatitudeWebPart.ModeTypes)Enum.Parse(typeof(LatitudeWebPart.ModeTypes), ddlModes.SelectedValue));
            }
        }

        void ddlSites_SelectedIndexChanged(object sender, EventArgs e)
        {

            LatitudeWebPart webPart = (LatitudeWebPart)this.ParentToolPane.SelectedWebPart;

            ddlLists.DataSource = webPart.Lists(new Guid(ddlSites.SelectedValue));
            ddlLists.DataTextField = "Title";
            ddlLists.DataValueField = "ID";

            ddlLists.DataBind();


            ddlFields.DataSource = webPart.Fields(new Guid(ddlSites.SelectedValue), new Guid(ddlLists.SelectedValue));
            ddlFields.DataTextField = "Title";
            ddlFields.DataValueField = "ID";

            ddlFields.DataBind();




            ddlGroups.DataSource = webPart.Groups(new Guid(ddlSites.SelectedValue));
            ddlGroups.DataTextField = "Name";
            ddlGroups.DataValueField = "ID";

            ddlGroups.DataBind();




        }

        void ddlDataSources_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetVisibilityRows();
        }

        void ddlModes_SelectedIndexChanged(object sender, EventArgs e)
        {

            LatitudeWebPart webPart = (LatitudeWebPart)this.ParentToolPane.SelectedWebPart;

            ddlDataSources.DataSource = webPart.DataSources((LatitudeWebPart.ModeTypes)Enum.Parse(typeof(LatitudeWebPart.ModeTypes), ddlModes.SelectedValue));
            ddlDataSources.DataTextField = "Name";
            ddlDataSources.DataValueField = "Type";

            ddlDataSources.DataBind();




            SetVisibilityRows();
        
        }
        
        void SetVisibilityRows()
        {
            rowSite.Visible = ((currentDataSource == LatitudeWebPart.DataSourceTypes.List) ||
                ((currentDataSource == LatitudeWebPart.DataSourceTypes.UserProfile) &&
                (currentMode == LatitudeWebPart.ModeTypes.MultipleUser)));

            rowList.Visible = (currentDataSource == LatitudeWebPart.DataSourceTypes.List);
            rowField.Visible = (currentDataSource == LatitudeWebPart.DataSourceTypes.List);
            rowNameFields.Visible = (currentDataSource == LatitudeWebPart.DataSourceTypes.List);
            
            rowLatitudeUserID.Visible = (currentDataSource == LatitudeWebPart.DataSourceTypes.Manual);
            rowManualDisplayName.Visible = (currentDataSource == LatitudeWebPart.DataSourceTypes.Manual);
            
            rowPropertyName.Visible = (currentDataSource == LatitudeWebPart.DataSourceTypes.UserProfile);
            
            rowGroups.Visible = ((currentDataSource == LatitudeWebPart.DataSourceTypes.UserProfile) &&
                 (currentUsersType == LatitudeWebPart.UsersTypes.Group));

            rowUser.Visible = ((currentDataSource == LatitudeWebPart.DataSourceTypes.UserProfile) &&
                     (currentMode == LatitudeWebPart.ModeTypes.SingleUser));

            rowUsers.Visible = ((currentDataSource == LatitudeWebPart.DataSourceTypes.UserProfile) &&
                    (currentMode == LatitudeWebPart.ModeTypes.MultipleUser));

        }

        public override void ApplyChanges()
        {
            LatitudeWebPart webPart = (LatitudeWebPart)this.ParentToolPane.SelectedWebPart;

            webPart.CurrentMapType = (LatitudeWebPart.MapTypes)Enum.Parse(typeof(LatitudeWebPart.MapTypes), ddlMapTypes.SelectedValue);
            webPart.BingMapsKey = tbBingMapsKey.Text;

            int width;

            if (int.TryParse(tbMapWidth.Text, out width))
                webPart.MapWidth = width;

            int height;

            if (int.TryParse(tbMapHeight.Text, out height))
                webPart.MapHeight = height;

            webPart.CurrentMode = currentMode;
            webPart.CurrentDataSource = currentDataSource;

            if (currentDataSource == LatitudeWebPart.DataSourceTypes.Manual)
            {
                webPart.LatitudeUserID = tbLatitudeUserID.Text;
                webPart.ManualDisplayName = tbManualDisplayName.Text;
            }

            if (currentDataSource == LatitudeWebPart.DataSourceTypes.List)
            {
                webPart.SourceSite = ddlSites.SelectedValue;
                webPart.SourceList = ddlLists.SelectedValue;
                webPart.SourceField = ddlFields.SelectedValue;
                webPart.SourceNameField = ddlNameFields.SelectedValue;
            }

            if (currentDataSource == LatitudeWebPart.DataSourceTypes.UserProfile)
                webPart.PropertyName = tbPropertyName.Text;

            if (currentDataSource == LatitudeWebPart.DataSourceTypes.UserProfile && currentMode == LatitudeWebPart.ModeTypes.SingleUser)
            {
                webPart.CurrentUser = currentUserType;

                if (currentUserType == LatitudeWebPart.UserTypes.Manual)
                    webPart.CurrentUserLoginName = ddlUser.SelectedValue;
            }

            if (currentDataSource == LatitudeWebPart.DataSourceTypes.UserProfile && currentMode == LatitudeWebPart.ModeTypes.MultipleUser)
            {
                webPart.CurrentUsers = currentUsersType;
                webPart.SourceSite = ddlSites.SelectedValue;

                if(currentUsersType == LatitudeWebPart.UsersTypes.Group)
                    webPart.SourceGroup = ddlGroups.SelectedValue;

            }

            
            base.ApplyChanges();
        }
    }
}
